/* Custom Unique Username Validation
 * Used in 01. Form Register */

var usernameErrLength = 'El nombre de usuario debe tener un m&iacute;nimo de 4 caracteres!';
var usernameErrUnique = 'Nombre de usuario existente!';
var usernameSuccess = 'Nombre de usuario disponible';
var emailErrFormat = 'Email no v&aacute;lido!';
var emailErrUnique = 'Email en uso!';
var emailSuccess = 'Email v&aacute;lido y disponible';

Ext.apply(Ext.form.VTypes, {
    uniqueusernameMask : /[a-z0-9_\.\-@\+]/i,
	uniqueusername : function(val) {
        if (val.length < 4) {
            Ext.apply(Ext.form.VTypes, {
                uniqueusernameText: usernameErrLength
            });
            return false;
        } else {
            Ext.Ajax.request({
                url: BASE_URL + 'users/isUniqueUsername',
                method: 'POST',
                params: 'username=' + val,
                success: function(o) {
                    if (o.responseText == 0) {
                        resetUsernameValidator(false);
                        Ext.apply(Ext.form.VTypes, {
                            uniqueusernameText: usernameErrUnique
                        });
                        return false;
                    } else {
                        resetUsernameValidator(true);
                    }
                }
            });
            return true;
        }
	},
	uniqueusernameText : usernameErrUnique,

    uniqueemailMask : /[a-z0-9_\.\-@\+]/i,
    uniqueemail : function(val) {
        var uniqueemail = /^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,6}$/;
        if (uniqueemail.test(val)) {
            Ext.Ajax.request({
                url: BASE_URL + 'users/isUniqueEmail',
                method: 'POST',
                params: 'email=' + val,
                success: function(o) {
                    if (o.responseText == 0) {
                        resetEmailValidator(false);
                        Ext.apply(Ext.form.VTypes, {
                            uniqueemailText: emailErrUnique
                        });
                    } else {
                        resetEmailValidator(true);
                    }
                }
            });
            return true;
        } else {
            return false;
        }

    },
    uniqueemailText: emailErrFormat,

    password: function(val, field) {
        if (field.initialPassField) {
            var pwd = Ext.getCmp(field.initialPassField);
            return (val == pwd.getValue());
        }
        return true;
    },
    passwordText: 'Contrase&ntilde;as no coinciden!',

    passwordlength: function(val) {
        if (val.length < 5) {
            return false;
        } else {
            return true;
        }
    },
    passwordlengthText: 'La contrase&ntilde;a debe de tener un m&iacute;nimo de 5 character',
    
    // guidocecilio - October 16th, 2010
    // TODO: we must find a solution to send to the handler the correct return once the Asychronic call is executed.
    currentPassword: function(val) {
    	var username = Ext.getCmp('regUsername').getValue();
        Ext.Ajax.request({
            url: BASE_URL + 'users/isPasswordValid',
            method: 'POST',
            params: 'username=' + username + '&password=' + hex_md5(val),
            success: function(response) {
        		var result = Ext.util.JSON.decode(response.responseText);
        		if (result.success) {
                	resetCurrentPasswordValidator(true);
                } 
        		else {
        			resetCurrentPasswordValidator(false);
        		}
            }
        });
        return false;
    },
    currentPasswordText: 'La contrase&ntilde;a es incorrecta!'
});

function resetUsernameValidator(is_error) {
	Ext.apply(Ext.form.VTypes, {
		uniqueusername : function(val) {
            if (val.length < 4) {
                Ext.apply(Ext.form.VTypes, {
                    uniqueusernameText: usernameErrLength
                });
                return false;
            } else {
                Ext.Ajax.request({
                    url: BASE_URL + 'users/isUniqueUsername',
                    method: 'POST',
                    params: 'username=' + val,
                    success: function(o) {
                        if (o.responseText == 0) {
                            resetUsernameValidator(false);
                            Ext.apply(Ext.form.VTypes, {
                            	uniqueusernameText: usernameErrUnique
                            });
                        } else {
                            resetUsernameValidator(true);
                        }
                    }
                });
                return is_error;
            }
		}
	});
}

function resetEmailValidator(value) {
    Ext.apply(Ext.form.VTypes, {
        uniqueemail : function(val) {
            var uniqueemail = /^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,6}$/;
            if (uniqueemail.test(val)) {
                Ext.Ajax.request({
                    url: BASE_URL + 'users/isUniqueEmail',
                    method: 'POST',
                    params: 'email=' + val,
                    success: function(o) {
                        if (o.responseText == 0) {
                            resetEmailValidator(false);
                            Ext.apply(Ext.form.VTypes, {
                                uniqueemailText: emailErrUnique
                            });
                        } else {
                            resetEmailValidator(true);
                        }
                    }
                });
            } else {
                return false;
            }
            return (value);
        }
    });
}

function resetCurrentPasswordValidator(value) {
    Ext.apply(Ext.form.VTypes, {
    	currentPassword: function(val) { 
    		var username = Ext.getCmp('regUsername').getValue();
	        Ext.Ajax.request({
	        	url: BASE_URL + 'users/isPasswordValid',
	            method: 'POST',
	            params: 'username=' + username + '&password=' + hex_md5(val),
	            success: function(response) {
	        		var result = Ext.util.JSON.decode(response.responseText);
	                if (result.success) {
	                	resetCurrentPasswordValidator(true);
	                } else {
	                	resetCurrentPasswordValidator(false);
	                }
	            }, 
	            scope: this
	        });
	        return value;
        }
    });
}
